package com.vrv.netmgr.assets;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONTokener;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

import com.vrv.common.GlobalConstants;
import com.vrv.common.UrlPrefix;
import com.vrv.common.listview.ListViewAdapter;
import com.vrv.common.listview.ListViewUtil;
import com.vrv.common.util.HttpClientToServer;
import com.vrv.common.util.JsonUtil;
import com.vrv.netmgr.R;
import com.vrv.netmgr.assets.detail.DetailSoftwareQryActivity;
import com.vrv.netmgr.vo.SerializableSoftwareQry;

/**
 * @Description 安装软件查询
 * @author gaopei
 * @date
 * 
 */
@SuppressLint("HandlerLeak")
public class SoftwareQryActivity extends Activity {

	// 存储数据的数组列表
	private ArrayList<HashMap<String, Object>> listData = new ArrayList<HashMap<String, Object>>();
	private ListView listView;
	private ListAdapter adapter = new ListAdapter();
	private ListViewItemClick listItemClk = new ListViewItemClick();
	private String UNIT_NAME;
	private String DEP_NAME;
	private String USER_NAME;
	private String TEL;
	private String IP_ADDRESS;
	private String SOFTWARE_COUNT;
	private String SOFTWARE_NAME;
	private String SOFTWARE_VER;
	private String INSTALL_COUNT;
	//列表的页脚视图
	private View loadMoreView;
	//搜索输入框
	private EditText edit;
	//搜索按钮
	private ImageView search_btn;
	/**
	 * 等待对话框
	 */
	private ProgressDialog progressDialog;	
	/**
	 * 查询
	 */
	private static final int QUERY = 3;
	private static final int NET_ERR = 4;
	private static final int QRY_BY_DEV = 5;
	private static final int QRY_BY_SW = 6;
	private static final int QUERY_BY_IP = 7;
	//查询到的记录总数
	public int RECORD_COUNT;
	//当前页号，从0开始算
	public int CUR_INDEX;
	//通过点击按钮查询标记
	private boolean qryByBtnFlag = false;
	// PopWindow检索条件相关
	private ImageButton moreFuncBtn;
	private PopupWindow pwMyPopWindow;
	private ListView lvPopupList;
	public final static String KEY_pageNum = "pageNum";
	public final static String KEY_byDeviceIP = "byDeviceIP";
	public final static String KEY_deviceIP = "deviceIP";
	public final static String KEY_pageDown = "pageDown";
	//当前查询类型
	private boolean curIsByDev = true;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
		setContentView(R.layout.assets_list_page);
		getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,
				R.layout.tools_title);
		getWindow().setSoftInputMode(
				WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
		init();		
		progressDialog = ProgressDialog.show(SoftwareQryActivity.this, "",
				"查询中...", true, true);
		
		new Thread(new Runnable() {
			@Override
			public void run() {
				Map<String, Object> argsMap = new HashMap<String, Object>();
				argsMap.put(KEY_pageNum, 0);
				argsMap.put(KEY_byDeviceIP, false);
				argsMap.put(KEY_deviceIP, null);
				getDataFromServer(argsMap);
				handler.sendEmptyMessage(QUERY);
			}
		}).start();
	}
	
	/**listview背景设置*/
	private LinearLayout listBackView;
	
	/**
	 * 处理器
	 */
	private Handler handler = new Handler() {
		public void handleMessage(Message msg) {
			progressDialog.dismiss();
			switch (msg.what) {
			case QUERY:
				findViews();
				/** listview背景设置 */
				listBackView.setVisibility(listView.getVisibility());
				break;
			case NET_ERR:
				Toast.makeText(SoftwareQryActivity.this, "网络连接失败",
						Toast.LENGTH_LONG).show();
				break;
			case QRY_BY_DEV:
				if(RECORD_COUNT == 0){
					listView.setVisibility(View.INVISIBLE);
					break;
				}else listView.setVisibility(View.VISIBLE);
				if (RECORD_COUNT > ListViewUtil.PAGESIZE){
					Map<String, Object> argsMap = new HashMap<String, Object>();
					argsMap.put(KEY_byDeviceIP, false);
					argsMap.put(KEY_deviceIP, null);
					loadMorePage(argsMap);
				}
				adapter.setByDev(true);
				listItemClk.setByDev(true);
				adapter.clear();
				initializeAdapter();
				//
				listView.setAdapter(adapter);
				edit.setEnabled(true);
				search_btn.setEnabled(true);
				adapter.notifyDataSetChanged();
				/** listview背景设置 */
				listBackView.setVisibility(listView.getVisibility());
				break;
			case QRY_BY_SW:
				if(RECORD_COUNT == 0){
					listView.setVisibility(View.INVISIBLE);
					break;
				}else listView.setVisibility(View.VISIBLE);
				if (RECORD_COUNT > ListViewUtil.PAGESIZE){
					Map<String, Object> argsMap = new HashMap<String, Object>();
					argsMap.put(KEY_byDeviceIP, false);
					argsMap.put(KEY_deviceIP, null);
					loadMorePage(argsMap);
				}
				adapter.setByDev(false);
				listItemClk.setByDev(false);
				adapter.clear();
				initializeAdapter();
				//
				listView.setAdapter(adapter);
				edit.setEnabled(false);
				search_btn.setEnabled(false);
				adapter.notifyDataSetChanged();
				/** listview背景设置 */
				listBackView.setVisibility(listView.getVisibility());
				break;
			case QUERY_BY_IP:
				qryByBtnFlag = true;
				if (RECORD_COUNT > 0) {
					listView.removeFooterView(loadMoreView);
					listView.setVisibility(View.VISIBLE);
					if (RECORD_COUNT > ListViewUtil.PAGESIZE){
						String deviceIP = msg.getData().getString("deviceIP");
						Map<String, Object> argsMap = new HashMap<String, Object>();
						argsMap.put(KEY_byDeviceIP, true);
						argsMap.put(KEY_deviceIP, deviceIP);
						loadMorePage(argsMap);
					}
					initializeAdapter();
					adapter.notifyDataSetChanged();
				} else listView.setVisibility(View.GONE);
				/** listview背景设置 */
				listBackView.setVisibility(listView.getVisibility());
				break;
			}
		}
	};
	
	private void init(){
		setTitle();
		UNIT_NAME = this.getString(R.string.unit_name);
		DEP_NAME = this.getString(R.string.dep_name);
		USER_NAME = this.getString(R.string.userName);
		TEL = this.getString(R.string.tel);
		IP_ADDRESS = this.getString(R.string.ip_address);
		SOFTWARE_COUNT = this.getString(R.string.software_count);
		SOFTWARE_NAME = this.getString(R.string.software_name);
		SOFTWARE_VER = this.getString(R.string.software_ver);
		INSTALL_COUNT = this.getString(R.string.install_count);		
		
		listBackView = (LinearLayout) findViewById(R.id.list_background);
	}

	public void setTitle() {
		TextView title = (TextView) findViewById(R.id.title_text);
		title.setText("安装软件查询");

		/**
		 * 返回按钮
		 */
		ImageButton backBtn = (ImageButton) findViewById(R.id.backBtn);
		backBtn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				onBackPressed();
			}
		});
	}

	public void findViews() {
		if(listView == null){
			edit = (EditText)findViewById(R.id.assets_conditionQry);
			search_btn = (ImageView)findViewById(R.id.search_go_btn);
			edit.setHint(R.string.input_device_ip);
//			edit.addTextChangedListener(new MyTextWatcher(edit, search_btn));
			search_btn.setOnClickListener(new ClickSearchBtnListener(edit, search_btn));
			iniPopupWindow();
			// 更多操作按钮
			moreFuncBtn = (ImageButton) findViewById(R.id.moreFunctionBtn);
			moreFuncBtn.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					if (pwMyPopWindow.isShowing()) {
						pwMyPopWindow.dismiss();// 关闭
					} else {
						pwMyPopWindow.showAsDropDown(moreFuncBtn);// 显示
					}
				}
			});
			listView = (ListView) findViewById(R.id.assets_lv_id);
		}		
		if (RECORD_COUNT > 0) {
			listView.removeFooterView(loadMoreView);
			listView.setVisibility(View.VISIBLE);
			if (RECORD_COUNT > ListViewUtil.PAGESIZE){
				Map<String, Object> argsMap = new HashMap<String, Object>();
				argsMap.put(KEY_byDeviceIP, false);
				argsMap.put(KEY_deviceIP, null);
				loadMorePage(argsMap);
			}
			adapter.setByDev(true);
			initializeAdapter();
			if (listView.getAdapter() == null)
				listView.setAdapter(adapter);
			else adapter.notifyDataSetChanged();
			listItemClk.setByDev(true);
			listView.setOnItemClickListener(listItemClk);
		}
	}
	
	private class ClickSearchBtnListener implements View.OnClickListener{
		EditText edit;
		ImageView search_btn;
		
		ClickSearchBtnListener(EditText edit, ImageView search_btn){
			this.edit = edit;
			this.search_btn = search_btn;
		}

		@Override
		public void onClick(View v) {
			final String editVal = edit.getText().toString();
			listData.clear();
			adapter.clear();
			progressDialog = ProgressDialog.show(SoftwareQryActivity.this, "",
					"查询中...", true, true);
			search_btn.setEnabled(false);
			new Handler().post(new Runnable() {
				@Override
				public void run() {
					Map<String, Object> argsMap = new HashMap<String, Object>();
					argsMap.put(KEY_pageNum, 0);
					argsMap.put(KEY_byDeviceIP, true);
					argsMap.put(KEY_deviceIP, editVal);
					getDataFromServer(argsMap);
					Message msg = new Message();
					msg.what = QUERY_BY_IP;
					Bundle data = new Bundle();
					data.putString("deviceIP", editVal);
					msg.setData(data);
					handler.sendMessage(msg);
					search_btn.setEnabled(true);
				}
			});
		}		
	}
	
//	private class MyTextWatcher implements TextWatcher{
//		EditText edit;
//		ImageButton search_btn;
//		
//		MyTextWatcher(EditText edit, ImageButton search_btn){
//			this.edit = edit;
//			this.search_btn = search_btn;
//		}
//
//		@Override
//		public void afterTextChanged(Editable s) {
//			String editVal = edit.getText().toString().trim();
//			if(StringUtils.isNotBlank(editVal))
//				search_btn.setVisibility(View.VISIBLE);
//			else {
//				search_btn.setVisibility(View.GONE);
//				if(qryByBtnFlag){
//					qryByBtnFlag = false;
//					listData.clear();
//					adapter.clear();
//					progressDialog = ProgressDialog.show(SoftwareQryActivity.this, "",
//							"查询中...", true, true);
//					
//					new Thread(new Runnable() {
//						@Override
//						public void run() {
//							Map<String, Object> argsMap = new HashMap<String, Object>();
//							argsMap.put(KEY_pageNum, 0);
//							argsMap.put(KEY_byDeviceIP, false);
//							argsMap.put(KEY_deviceIP, null);
//							getDataFromServer(argsMap);
//							handler.sendEmptyMessage(QUERY);
//						}
//					}).start();
//				}
//			}
//		}
//
//		@Override
//		public void beforeTextChanged(CharSequence s, int start, int count,
//				int after) {
//			
//		}
//
//		@Override
//		public void onTextChanged(CharSequence s, int start, int before,
//				int count) {
//			
//		}
//		
//	}

	private class ListViewItemClick implements AdapterView.OnItemClickListener {
		private boolean isByDev;

		public void setByDev(boolean isByDev) {
			this.isByDev = isByDev;
		}

		@Override
		public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
				long arg3) {
			if (isByDev) {
				HashMap<String, Object> hashMap = adapter.getData(arg2);
				//
				SerializableSoftwareQry serializable = new SerializableSoftwareQry();
				serializable.setUNIT_NAME(hashMap.get(UNIT_NAME).toString());
				serializable.setDEP_NAME(hashMap.get(DEP_NAME).toString());
				serializable.setUSER_NAME(hashMap.get(USER_NAME).toString());
				serializable.setTEL(hashMap.get(TEL).toString());
				serializable.setUSER_NAME(hashMap.get(USER_NAME).toString());
				serializable.setIP_ADDRESS(hashMap.get(IP_ADDRESS).toString());
				serializable.setSOFTWARE_COUNT(hashMap.get(SOFTWARE_COUNT).toString());
				//
				Intent intent = new Intent(SoftwareQryActivity.this, DetailSoftwareQryActivity.class);
				Bundle bundle = new Bundle();
				bundle.putSerializable("info", serializable);
				intent.putExtras(bundle);
				startActivity(intent);
			}
		}

	}

	/**
	 * 初始化ListView的适配器
	 */
	public void initializeAdapter() {

		for (int i = 0; i < listData.size(); i++)
			adapter.addItem(listData.get(i));
	}

	public void loadMorePage(Map<String, Object> argsMap) {
		final boolean byDeviceIP = (Boolean)argsMap.get(KEY_byDeviceIP);
		final String deviceIP = (String)argsMap.get(KEY_deviceIP);
		loadMoreView = getLayoutInflater().inflate(R.layout.list_page_load,
				null);
		final Button loadNextButton = (Button) loadMoreView.findViewById(R.id.loadNextButton);
		final Button loadPreButton = (Button) loadMoreView.findViewById(R.id.loadPreButton);
		loadNextButton.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				if (loadNextButton.getText().equals("查看下一页")) {
					loadNextButton.setText("正在加载中..."); // 设置按钮文字
					loadNextButton.setEnabled(false);
					new Handler().postDelayed(new Runnable() {

						@Override
						public void run() {
							Map<String, Object> argsMap = new HashMap<String, Object>();
							argsMap.put(KEY_pageDown, true);
							argsMap.put(KEY_byDeviceIP, byDeviceIP);
							argsMap.put(KEY_deviceIP, deviceIP);
							loadMoreData(argsMap);
							adapter.notifyDataSetChanged();
							loadNextButton.setText("查看下一页"); // 恢复按钮文字
							if ((CUR_INDEX + 1) * ListViewUtil.PAGESIZE >= RECORD_COUNT)
								loadNextButton.setVisibility(View.GONE);
							if (CUR_INDEX == 1)
								loadPreButton.setVisibility(View.VISIBLE);
							loadNextButton.setEnabled(true);
						}
					}, 0);
				}
			}
		});
		loadPreButton.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				if (loadPreButton.getText().equals("查看上一页")) {
					loadPreButton.setText("正在加载中..."); // 设置按钮文字
					loadPreButton.setEnabled(false);
					new Handler().postDelayed(new Runnable() {

						@Override
						public void run() {
							Map<String, Object> argsMap = new HashMap<String, Object>();
							argsMap.put(KEY_pageDown, false);
							argsMap.put(KEY_byDeviceIP, byDeviceIP);
							argsMap.put(KEY_deviceIP, deviceIP);
							loadMoreData(argsMap);
							adapter.notifyDataSetChanged();
							loadPreButton.setText("查看上一页"); // 恢复按钮文字
							if (CUR_INDEX == 0)
								loadPreButton.setVisibility(View.GONE);
							if ((CUR_INDEX + 2) * ListViewUtil.PAGESIZE >= RECORD_COUNT)
								loadNextButton.setVisibility(View.VISIBLE);
							loadPreButton.setEnabled(true);
						}
					}, 0);
				}
			}
		});
		listView.addFooterView(loadMoreView); // 设置列表底部视图
	}

	/*
	 * 加载更多数据
	 */
	private void loadMoreData(Map<String, Object> argsMap) {
		int index;
		boolean pageDown = (Boolean)argsMap.get(KEY_pageDown);
		boolean byDeviceIP = (Boolean)argsMap.get(KEY_byDeviceIP);
		String deviceIP = (String)argsMap.get(KEY_deviceIP);
		if (pageDown)
			index = CUR_INDEX + 1;
		else
			index = CUR_INDEX - 1;
		listData.clear();
		adapter.clear();
		Map<String, Object> myMap = new HashMap<String, Object>();
		myMap.put(KEY_pageNum, index);
		myMap.put(KEY_byDeviceIP, byDeviceIP);
		myMap.put(KEY_deviceIP, deviceIP);
		getDataFromServer(myMap);
		int datasize = listData.size();
		for (int i = 0; i < datasize; i++)
			adapter.addItem(listData.get(i));
	}

	private void iniPopupWindow() {
        LayoutInflater inflater = (LayoutInflater) this
				.getSystemService(LAYOUT_INFLATER_SERVICE);
		View layout = inflater.inflate(R.layout.title_func_popupwindow, null);
		lvPopupList = (ListView) layout.findViewById(R.id.lv_popup_list);
		pwMyPopWindow = new PopupWindow(layout);
		pwMyPopWindow.setFocusable(true);// 加上这个popupwindow中的ListView才可以接收点击事件
		
		String[] arr = getResources().getStringArray(R.array.softwareSearchCondition);
		if (null == itemDatas) {
			itemDatas = new ArrayList();
		} else {
			itemDatas.clear();
		}
		for (int i = 0; i < arr.length; i++) {
			PopItem item = new PopItem();
			item.title = arr[i];
			item.check = false;
			if (0 == i) {
				item.check = true;
			}
			itemDatas.add(item);
		}
		if (null == pAdapter) {
			pAdapter = new PopWindowAdapter(itemDatas, SoftwareQryActivity.this);
		}
		lvPopupList.setAdapter(pAdapter);
		lvPopupList.setOnItemClickListener(new ClickPopupItemListener());
		
		// 控制popupwindow的宽度和高度自适应
		lvPopupList.measure(View.MeasureSpec.UNSPECIFIED,
				View.MeasureSpec.UNSPECIFIED);
		pwMyPopWindow.setWidth(lvPopupList.getMeasuredWidth());
		// pwMyPopWindow.setHeight((lvPopupList.getMeasuredHeight()) * 3);
		pwMyPopWindow.setHeight(LayoutParams.WRAP_CONTENT);

		// 控制popupwindow点击屏幕其他地方消失
		pwMyPopWindow.setBackgroundDrawable(this.getResources().getDrawable(
				R.drawable.bg_popupwindow));
		// 触摸popupwindow外部，popupwindow消失。这个要求你的popupwindow要有背景图片才可以成功，如上
		pwMyPopWindow.setOutsideTouchable(true);
	}
	
	private class ClickPopupItemListener implements AdapterView.OnItemClickListener{
		@Override
		public void onItemClick(AdapterView<?> parent, View view,
				final int position, long id) {
			pwMyPopWindow.dismiss();
			if (loadMoreView != null)
				listView.removeFooterView(loadMoreView);
			listData.clear();
			if (position == 0) {
				curIsByDev = true;
				progressDialog = ProgressDialog.show(
						SoftwareQryActivity.this, "", "查询中...", true, true);
				new Thread(new Runnable() {
					@Override
					public void run() {
						Map<String, Object> argsMap = new HashMap<String, Object>();
						argsMap.put(KEY_pageNum, 0);
						argsMap.put(KEY_byDeviceIP, false);
						argsMap.put(KEY_deviceIP, null);
						getDataFromServer(argsMap);
						handler.sendEmptyMessage(QRY_BY_DEV);
					}
				}).start();
			} else if(position == 1){
				curIsByDev = false;
				progressDialog = ProgressDialog.show(
						SoftwareQryActivity.this, "", "查询中...", true, true);
				new Thread(new Runnable() {
					@Override
					public void run() {
						Map<String, Object> argsMap = new HashMap<String, Object>();
						argsMap.put(KEY_pageNum, 0);
						argsMap.put(KEY_byDeviceIP, false);
						argsMap.put(KEY_deviceIP, null);
						getDataFromServer(argsMap);
						handler.sendEmptyMessage(QRY_BY_SW);
					}
				}).start();
			}
			
			for (int i = 0; i < itemDatas.size(); i++) {
				if (position == i) {
					itemDatas.get(i).check = true;
				} else {
					itemDatas.get(i).check = false;
				}
			}
			pAdapter.notifyDataSetChanged();
//            Toast.makeText(SoftwareQryActivity.this, "" + itemDatas.get(position).title, Toast.LENGTH_LONG).show();
		}
	}

    public List<PopItem> itemDatas;
    public PopWindowAdapter pAdapter;
    
    public class PopWindowAdapter extends BaseAdapter{
    	public List<PopItem> lst;
    	public Context mContext;
    	
    	public PopWindowAdapter(List<PopItem> lst, Context context){
    		this.lst = lst;
    		this.mContext = context;
    	}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return lst.size();
		}

		@Override
		public Object getItem(int arg0) {
			// TODO Auto-generated method stub
			return lst.get(arg0);
		}

		@Override
		public long getItemId(int arg0) {
			// TODO Auto-generated method stub
			return arg0;
		}

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if (convertView == null){
                convertView = LayoutInflater.from(SoftwareQryActivity.this).inflate(R.layout.list_item_popupwindow, null);
            }
            CheckBox cBox = (CheckBox) convertView.findViewById(R.id.check);
            TextView title =(TextView) convertView.findViewById(R.id.tv_list_item);
            
            cBox.setChecked(lst.get(position).check);
            title.setText(lst.get(position).title);
			
			return convertView;
		}
    	
    }
    
	public class PopItem {
		public String title;
		public boolean check = false;
	} 
	
	// 查询所有数据
	public void getDataFromServer(Map<String, Object> argsMap) {
		int pageNum = (Integer)argsMap.get(KEY_pageNum);
		boolean byDeviceIP = (Boolean)argsMap.get(KEY_byDeviceIP);
		String deviceIP = (String)argsMap.get(KEY_deviceIP);
		Map<String, String> map = new HashMap<String, String>();
		map.put(GlobalConstants.PAGE_NUM, pageNum + "");
		if(byDeviceIP)
			map.put(GlobalConstants.DEVICE_IP, deviceIP);
		boolean retVal = qryInstalledSW(map, curIsByDev);
		if (!retVal)
			handler.sendEmptyMessage(NET_ERR);
	}

	public boolean qryInstalledSW(Map<String, String> map, boolean isByDev) {
		boolean retVal = false;
		HttpClientToServer httpClientToServer = new HttpClientToServer();
		String urlPrefix = new UrlPrefix(getApplicationContext())
				.getUrlPrefix();
		String urlAddr = urlPrefix + "/deviceInfo/"
				+ (isByDev ? "jsonDeviceList.do" : "jsonSoftwareList.do");
		String response = httpClientToServer.doPost(urlAddr, map);
		if (HttpClientToServer.DO_POST_ERR.equals(response)
				|| HttpClientToServer.DO_POST_EXCEPT.equals(response))
			return retVal;
		try {
			JSONTokener jsonParser = new JSONTokener(response);
			JSONObject jsonObj = (JSONObject) jsonParser.nextValue();
			retVal = jsonObj.getBoolean("success");
            if (!retVal) return retVal;
			int count = jsonObj.getInt("totalRecords");
            RECORD_COUNT = count;
			if (count < 1) return retVal;
			JSONArray jArr = jsonObj.getJSONArray("records");
			int jArrLen = jArr.length();
            if (jArrLen > 0) 
				CUR_INDEX = Integer.parseInt(map.get(GlobalConstants.PAGE_NUM));
            for (int i = 0; i < jArr.length(); i++) {
				JSONObject jObj = (JSONObject) jArr.opt(i);
				HashMap<String, Object> myMap = new HashMap<String, Object>();
				if (isByDev) {
					myMap.put(UNIT_NAME, JsonUtil.getJsonString(jObj, "deptName"));
					myMap.put(DEP_NAME, JsonUtil.getJsonString(jObj, "officeName"));
					myMap.put(USER_NAME, JsonUtil.getJsonString(jObj, "userName"));
					myMap.put(TEL, JsonUtil.getJsonString(jObj, "tel"));
					myMap.put(IP_ADDRESS, JsonUtil.getJsonString(jObj, "ipaddres"));
					myMap.put(SOFTWARE_COUNT, JsonUtil.getJsonString(jObj, "reserved3"));
				} else {
					myMap.put(SOFTWARE_NAME, JsonUtil.getJsonString(jObj, "softwareName"));
					myMap.put(SOFTWARE_VER, JsonUtil.getJsonString(jObj, "versionInfo"));
					myMap.put(INSTALL_COUNT, JsonUtil.getJsonString(jObj, "clientSetupNum"));
				}
				listData.add(myMap);
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return retVal;
	}
	
	private class ListAdapter extends ListViewAdapter {
		private boolean isByDev;

		public void setByDev(boolean isByDev) {
			this.isByDev = isByDev;
		}

		@Override
		public View getView(int position, View view, ViewGroup parent) {
			final HashMap<String, Object> hashMap = getData(position);
			
            if (isByDev) {
				view = LayoutInflater.from(SoftwareQryActivity.this).inflate(R.layout.softwareqry_listpage_item_dev, null);
				/**默认背景色设置*/
				view.setBackgroundResource(R.drawable.list_item_state1);
				/**双行背景色设置*/
				if (position % 2 == 0) {
					view.setBackgroundResource(R.drawable.list_item_state);
				}
				TextView tv1 = (TextView) view.findViewById(R.id.tv_devip);
				TextView tv2 = (TextView) view.findViewById(R.id.tv_devname);
				TextView tv3 = (TextView) view.findViewById(R.id.tv_user);
				tv1.setText(hashMap.get(IP_ADDRESS).toString());
				tv2.setText(hashMap.get(SOFTWARE_COUNT).toString());
				tv3.setText(hashMap.get(USER_NAME).toString());
			} else {
				view = LayoutInflater.from(SoftwareQryActivity.this).inflate(R.layout.softwareqry_listpage_item, null);
				/**默认背景色设置*/
				view.setBackgroundResource(R.drawable.list_item_state1);
				/**双行背景色设置*/
				if (position % 2 == 0) {
					view.setBackgroundResource(R.drawable.list_item_state);
				}
				TextView tv1 = (TextView) view.findViewById(R.id.tv_devip);
				TextView tv2 = (TextView) view.findViewById(R.id.tv_version);
				TextView tv3 = (TextView) view.findViewById(R.id.tv_devname);
				tv1.setText(hashMap.get(SOFTWARE_NAME).toString());
				tv2.setText(hashMap.get(SOFTWARE_VER).toString());
				tv3.setText(hashMap.get(INSTALL_COUNT).toString());
			}

			return view;
		}
	}

	@Override
	public void onBackPressed() {
		finish();
		overridePendingTransition(R.anim.push_right_out, R.anim.push_right_in);
		super.onBackPressed();
	}
}
